define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'litestoregoods'], function($, undefined, Backend, Table, Form, Template, litestoregoods) {

    var Controller = {
        index: function() {
            $(".btn-add").data("area", ["1000px", "800px"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/litestoregoodsspec/index',
                    add_url: 'litestore/litestoregoods/add',
                    edit_url: 'litestore/litestoregoods/edit',
                    del_url: 'litestore/litestoregoods/del',
                    multi_url: 'litestore/litestoregoods/multi',
                    table: 'litestoregoods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_sort',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'goods_spec_id', title: __('id') },
                        { field: 'goods_id', title: __('Goods_id') },
                      
                        { field: 'good.goods_name', title: __('Goods_name'), operate: 'LIKE' },
                        { field: 'miaoshu', title: __('规格项'), operate: 'LIKE' },
                        { field: 'good.tags', title: __('关键字'), operate: 'LIKE', formatter: Table.api.formatter.flag },
                        { field: 'good.image', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'good.images', title: __('Images'), operate: false, formatter: Table.api.formatter.images },
                        { field: 'goods_price', title: __('价格'), operate: 'BETWEEN' },
                        { field: 'goods_price2', title: __('会员价'), operate: 'BETWEEN' },
                        { field: 'line_price', title: __('划线价'), operate: 'BETWEEN' },
                        { field: 'good.spec_type', title: __('Spec_type'), searchList: { "10": __('Spec_type 10'), "20": __('Spec_type 20') }, formatter: Table.api.formatter.normal },
                        { field: 'good.goods_status', title: __('Goods_status'), searchList: { "10": __('Goods_status 10'), "20": __('Goods_status 20') }, formatter: Table.api.formatter.status },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            clickToSelect: false,
                            buttons: [{
                                    'name': 'player',
                                    'title': function(row) {
                                        return '报价详情';
                                    },
                                    'icon': 'fa ',
                                    'text': '报价详情',
                                    'classname': 'btn btn-xs btn-info btn-dialog',
                                    'url': 'litestore/litestoregoodsspec/edit2/spcid/{goods_spec_id}',
                                    'extend': 'data-area=\'["55%","55%"]\'',

                                }

                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function(data) {
                $(".btn-editone").data("area", ["100%", "800px"]);
            });

        },

        index3: function() {
            $(".btn-add").data("area", ["1000px", "800px"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/litestoregoodsspec/index3',
                    add_url: 'litestore/litestoregoods/add',
                    edit_url: 'litestore/litestoregoods/edit',
                    del_url: 'litestore/litestoregoods/del',
                    multi_url: 'litestore/litestoregoods/multi',
                    table: 'litestoregoods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_sort',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'goods_spec_id', title: __('id') },
                        { field: 'goods_id', title: __('Goods_id') },
                        { field: 'good.goods_name', title: __('Goods_name'), operate: 'LIKE' },
                        { field: 'miaoshu', title: __('规格项'), operate: 'LIKE' },
                        { field: 'good.tags', title: __('关键字'), operate: 'LIKE', formatter: Table.api.formatter.flag },
                        { field: 'good.image', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'good.images', title: __('Images'), operate: false, formatter: Table.api.formatter.images },
                        { field: 'goods_price', title: __('价格'), operate: 'BETWEEN' },
                        { field: 'goods_price2', title: __('会员价'), operate: 'BETWEEN' },
                        { field: 'line_price', title: __('划线价'), operate: 'BETWEEN' },
                        { field: 'good.spec_type', title: __('Spec_type'), searchList: { "10": __('Spec_type 10'), "20": __('Spec_type 20') }, formatter: Table.api.formatter.normal },
                        { field: 'good.goods_status', title: __('Goods_status'), searchList: { "10": __('Goods_status 10'), "20": __('Goods_status 20') }, formatter: Table.api.formatter.status },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            clickToSelect: false,
                            buttons: [{
                                    'name': 'player',
                                    'title': function(row) {
                                        return '报价详情';
                                    },
                                    'icon': 'fa ',
                                    'text': '报价详情',
                                    'classname': 'btn btn-xs btn-info btn-dialog',
                                    'url': 'litestore/litestoregoodsspec/edit3/spcid/{goods_spec_id}',
                                    'extend': 'data-area=\'["60%","60%"]\'',

                                }

                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function(data) {
                $(".btn-editone").data("area", ["100%", "800px"]);
            });

        },

        index2: function() {
            $(".btn-add").data("area", ["1000px", "800px"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/litestoregoodsspec/index2',
                    add_url: 'litestore/litestoregoods/add',
                    edit_url: 'litestore/litestoregoods/edit',
                    del_url: 'litestore/litestoregoods/del',
                    multi_url: 'litestore/litestoregoods/multi',
                    table: 'litestoregoods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_sort',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'goods_id', title: __('商品ID') },
                        { field: 'good.goods_name', title: __('商品名称'), operate: 'LIKE' },
                        { field: 'spec_name', title: __('规格项'), operate: 'LIKE' },
                        { field: 'good.image', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'good.images', title: __('缩略图'), operate: false, formatter: Table.api.formatter.images },
                        { field: 'goods_price', title: __('价格'), operate: 'BETWEEN' },
                        { field: 'goods_price2', title: __('会员价'), operate: 'BETWEEN' },
                        { field: 'goods_price4', title: __('成本'), operate: 'BETWEEN' },
                        { field: 'daoqi', title: __('到期时间'), operate: 'BETWEEN' },

                        { field: 'good.spec_type', title: __('Spec_type'), searchList: { "10": __('Spec_type 10'), "20": __('Spec_type 20') }, formatter: Table.api.formatter.normal },

                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function(data) {
                $(".btn-editone").data("area", ["100%", "800px"]);
            });

        },

        chosegoods: function() {

            Table.api.init({
                extend: {
                    index_url: 'litestore/litestoregoodsspec/chosegoods' + location.search,
                    add_url: 'litestore/litestoregoods/add',
                    edit_url: 'litestore/litestoregoods/edit',
                    del_url: 'litestore/litestoregoods/del',
                    multi_url: 'litestore/litestoregoods/multi',
                    table: 'litestoregoods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_spec_id',
                sortName: 'goods_sort',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'goods_spec_id', title: __('商品ID') },
                        { field: 'good.category_id', title: __('分类ID'), visible: false, operate: 'FIND_IN_SET' },
                        { field: 'good.image', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'good.goods_name', title: __('商品名称'), operate: 'LIKE' },
                        { field: 'spec_name', title: __('规格项'), operate: 'LIKE' },
                        { field: 'danwei', title: __('单位'), operate: 'LIKE' },
                        { field: 'goods_price', title: __('价格'), operate: 'BETWEEN' },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function(data) {
                $(".btn-editone").data("area", ["100%", "800px"]);
            });

            require(['cms-simtree', 'cookie'], function() {
                var expandall = ($.cookie('cms-expandall') || $("#expandall").prop("checked").toString()) === 'true';
                var multiselect = ($.cookie('cms-multiselect') || $("#multiselect").prop("checked").toString()) === 'true';
                var linkselect = ($.cookie('cms-linkselect') || $("#linkselect").prop("checked").toString()) === 'true';

                if (!expandall) {
                    $(Config.channelList).each(function(i, j) {
                        Config.channelList[i].expand = false;
                    });
                }

                var tree = $("#channeltree").simTree({
                    el: '#channeltree',
                    data: $.extend(true, [], Config.channelList),
                    check: multiselect,
                    linkParent: linkselect, //父子关联
                    response: {
                        name: 'text',
                        pid: 'parent',
                    },
                    onClick: function(item) {},
                    onChange: function(item) {
                        var ids = [];
                        $(this.sels).each(function(i, j) {
                            ids.push(j.id);
                        });
                        $(".commonsearch-table input[name='good.category_id']").val(ids.join(","));

                        table.bootstrapTable('refresh', {});
                        // Fast.api.ajax({
                        //     url: "cms/archives/get_channel_model_info",
                        //     loading: false,
                        //     type: 'post',
                        //     data: { ids: ids.join(",") }
                        // }, function(data, ret) {
                        //     if (parseInt(model_id) !== parseInt(data.model_id)) {
                        //         model_id = parseInt(data.model_id);
                        //         var columns = column.concat([]);
                        //         $(columns).each(function(i, j) {
                        //             if (j.field === 'channel_id') {
                        //                 columns[i]['defaultValue'] = ids.join(",");
                        //             }
                        //         });
                        //         //动态追加字段
                        //         columns = columns.concat(Base.api.getCustomFields(data.fields, table));
                        //         //动态追加操作列
                        //         columns = columns.concat(operate);

                        //         var searchFormVisible = !$(".commonsearch-table").hasClass("hidden");
                        //         //重新初始化表格
                        //         table.bootstrapTable("destroy").bootstrapTable({
                        //             url: $.fn.bootstrapTable.defaults.extend.index_url + "?model_id=" + data.model_id,
                        //             pk: 'id',
                        //             sortName: 'id',
                        //             fixedColumns: true,
                        //             fixedRightNumber: 2,
                        //             searchFormVisible: searchFormVisible,
                        //             columns: columns
                        //         });
                        //     } else {
                        //         $(".commonsearch-table input[name=channel_id]").selectPageRefresh();
                        //         table.bootstrapTable('refresh', {});
                        //     }
                        //     return false;
                        // });
                        return false;
                    }
                });

                //多选模式
                $("#multiselect").prop("checked", multiselect);
                $(document).on("change", "#multiselect", function() {
                    tree.options.check = $(this).prop("checked");
                    tree.$el.find(".sim-tree-checkbox").removeClass("checked");
                    tree.sels = [];
                    tree.trigger('change', []);
                    // $("#channeltree .sim-tree-checkbox").toggleClass("sim-hide", !tree.options.check);
                    $.cookie('cms-multiselect', $(this).prop("checked"));
                });

                //联动模式
                $("#linkselect").prop("checked", linkselect);
                $(document).on("change", "#linkselect", function() {
                    tree.options.linkParent = $(this).prop("checked");
                    tree.refresh($.extend(true, [], Config.channelList));
                    tree.trigger('change', []);
                    // $("#channeltree .sim-tree-checkbox").toggleClass("sim-hide", !tree.options.check);
                    $.cookie('cms-linkselect', $(this).prop("checked"));
                });

                // 展开全部
                $("#expandall").prop("checked", expandall);
                $(document).on("change", "#expandall", function() {
                    if ($(this).prop("checked")) {
                        $(".sim-tree-spread.sim-icon-r", tree.$el).trigger("click");
                    } else {
                        $(".sim-tree-spread.sim-icon-d", tree.$el).trigger("click");
                    }
                    $.cookie('cms-expandall', $(this).prop("checked"));
                });
            });


            $(document).on('click', '.btn-check', function() {
                let ids = Table.api.selectedids(table);
                Fast.api.close(ids);
            });

        },
        edit2: function() {
            Controller.api.bindevent();
        },
        edit3: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});